<template>
  <el-dialog :title="auditTitle" v-model="visible" width="1000px" append-to-body @close="handleClose">
    <el-form ref="auditRef" :model="auditForm" :rules="auditRules" label-width="80px">
      <el-form-item label="审核结果" prop="auditStatus">
        <el-radio-group v-model="auditForm.auditStatus">
          <el-radio
            v-for="dict in withdraw_audit_status.filter(item => item.value !== '0')"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核备注" prop="auditRemark">
        <el-input
          v-model="auditForm.auditRemark"
          type="textarea"
          placeholder="请输入审核备注"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    
    <!-- 审核时也显示关联账单列表 -->
    <associate-bills-table 
      :withdraw-id="withdrawId" 
      v-if="withdrawId"
    />
    
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitAudit" :loading="loading">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { auditWithdraw } from "@/api/apartment/withdraw"
import AssociateBillsTable from './AssociateBillsTable.vue'

const { proxy } = getCurrentInstance()
const { withdraw_audit_status } = proxy.useDict('withdraw_audit_status')

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  auditTitle: {
    type: String,
    default: '审核提现申请'
  },
  withdrawId: {
    type: [String, Number],
    default: null
  },
  withdrawIds: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['update:modelValue', 'success'])

const visible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
})

const loading = ref(false)
const auditRef = ref()

const auditForm = reactive({
  auditStatus: "1",
  auditRemark: ""
})

const auditRules = {
  auditStatus: [{ required: true, message: "请选择审核结果", trigger: "change" }]
}

const submitAudit = () => {
  auditRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      
      // 如果是批量操作，需要逐个处理
      if (props.withdrawIds.length > 1) {
        // 批量审核
        const promises = props.withdrawIds.map(withdrawId => 
          auditWithdraw(withdrawId, auditForm)
        )
        Promise.all(promises).then(() => {
          proxy.$modal.msgSuccess("批量审核成功")
          handleClose()
          emit('success')
        }).catch(() => {
          proxy.$modal.msgError("批量审核失败")
        }).finally(() => {
          loading.value = false
        })
      } else {
        // 单个审核
        auditWithdraw(props.withdrawId, auditForm).then(response => {
          proxy.$modal.msgSuccess("审核成功")
          handleClose()
          emit('success')
        }).catch(() => {
          proxy.$modal.msgError("审核失败")
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

const handleClose = () => {
  auditForm.auditStatus = "1"
  auditForm.auditRemark = ""
  emit('update:modelValue', false)
}

// 监听审核状态变化，自动设置默认值
watch(() => props.auditTitle, (newTitle) => {
  if (newTitle.includes('批准')) {
    auditForm.auditStatus = "1"
  } else if (newTitle.includes('拒绝')) {
    auditForm.auditStatus = "2"
  }
})
</script>

<style scoped>
.amount-text {
  color: #f56c6c;
  font-weight: bold;
}
</style> 